using Digitalmes.Application.Mes.Processes.Routings;
using Digitalmes.Application.Mes.Processes.Routings.Commands;
using Digitalmes.Application.Mes.Processes.Routings.Queries;

namespace Digitalmes.WebApi.Endpoints.Mes.Processes;

/// <summary>
/// 工艺路线结点。
/// </summary>
public sealed class RoutingEndpoint : EndpointGroupBase
{
    public override void Map(WebApplication app)
    {
        app.MapGroup(this, "mes/process", "routing")
            .RequireAuthorization()
            .MapGet(GetRoutingListAsync, "list")
            .MapGet(GetRoutingPagedListAsync, "list-page")
            .MapPost(CreateRoutingAsync)
            .MapPost(CopyRoutingAsync, "copy")
            .MapPut(UpdateRoutingAsync, "{id}")
            .MapDelete(DeleteRoutingAsync, "{id}");
    }

    [EndpointDescription("查询工艺路线集合")]
    public async Task<IApiResult> GetRoutingListAsync(ISender sender, [AsParameters] GetRoutingListQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("分页查询工艺路线")]
    public async Task<IApiResult> GetRoutingPagedListAsync(ISender sender, [AsParameters] GetRoutingListWithPagedQuery query)
    {
        var result = await sender.Send(query);
        return result.ToApiResult();
    }

    [EndpointDescription("创建工艺路线")]
    public async Task<IApiResult> CreateRoutingAsync(ISender sender, ProcRoutingDto dto)
    {
        var result = await sender.Send(new CreateRoutingCommand(dto));
        return result.ToApiResult();
    }

    [EndpointDescription("复制一份工艺路线数据")]
    public async Task<IApiResult> CopyRoutingAsync(ISender sender, CopyRoutingCommand command)
    {
        var result = await sender.Send(command);
        return result.ToApiResult();
    }

    [EndpointDescription("更新工艺路线")]
    public async Task<IApiResult> UpdateRoutingAsync(ISender sender, long id, ProcRoutingDto dto)
    {
        var result = await sender.Send(new UpdateRoutingCommand(id, dto));
        return result.ToApiResult();
    }

    [EndpointDescription("删除工艺路线")]
    public async Task<IApiResult> DeleteRoutingAsync(ISender sender, long id)
    {
        var result = await sender.Send(new DeleteRoutingCommand(id));
        return result.ToApiResult();
    }
}
